package oop.lab.console.gameview;

import java.util.List;

import oop.lab.console.BoardViewer;
import oop.lab.console.command.Command;
import oop.lab.console.command.NewGameCommand;
import oop.lab.console.command.PassCommand;
import oop.lab.console.command.PlacePieceCommand;
import oop.lab.console.command.QuitCommand;
import oop.lab.console.command.SelectGameCommand;
import oop.lab.console.playground.PlayGround;
import oop.lab.console.playground.Player;
import oop.lab.console.screen.Screen;
import oop.lab.domain.board.Location;
import oop.lab.domain.board.PieceColor;
import oop.lab.domain.game.ReversiGame;

public class ReversiGameView extends GameViewer {
    private final ReversiGame game;

    public static ReversiGameView create() {
        return new ReversiGameView(new ReversiGame());
    }

    public static ReversiGameView createWithFinalState() {
        return new ReversiGameView(ReversiGame.ofGameWithFinishState(), "结束状态测试");
    }

    public ReversiGameView(ReversiGame game, String gameDesc) {
        super(gameDesc);
        this.game = game;
    }

    public ReversiGameView(ReversiGame game) {
        this(game, "");
    }

    @Override
    public ReversiGame getGame() {
        return game;
    }

    public void showBoard(Screen screen) {
        // 先用BoardUI显示棋盘
        BoardViewer boardUI = new BoardViewer(game.getBoard());
        boardUI.display(screen);

        // 获取当前玩家所有合法落子点
        PieceColor currentPiece = game.getCurrentPlayer();
        List<Location> validMoves = game.getAllValidLocation(currentPiece);

        // 在合法落子点位置覆盖为'+'
        for (Location loc : validMoves) {
            // BoardUI的棋盘内容起始行为1，起始列为2+2*y
            screen.print(loc.getX() + 1, 2 + loc.getY() * 2, '+');
        }
    }

    @Override
    public String getGameStatus() {
        if (game.isOver()) {
            if (game.getWinner() == null)
                return "游戏结束：平局";
            else {
                // 获取比分
                int blackCount = game.getPieceCount(PieceColor.BLACK);
                int whiteCount = game.getPieceCount(PieceColor.WHITE);
                return String.format("游戏结束，%s获胜(%d:%d)", game.getWinner(), blackCount, whiteCount);
            }
        } else if (game.shouldPass()) {
            return "当前玩家无法落子,请pass跳过";
        } else {
            return "";
        }
    }

    public String getPlayerStatus(int index, Player player, boolean isCurrentPlayer, boolean gameOver) {
        String playerStatus = super.getPlayerStatus(index, player, isCurrentPlayer, gameOver);
        // 玩家状态信息中显示棋子数量
        int count = game.getPieceCount(player.getPieceColor());
        return playerStatus + " " + count;
    }

    @Override
    public List<Command> getCommandList(PlayGround session) {
        return List.of(
                new PlacePieceCommand(game, session) {
                            @Override
                    public boolean isEnabled() {
                        return !game.isOver() && !game.shouldPass();
                    }
                },
                new SelectGameCommand(session),
                new NewGameCommand(session),
                new PassCommand(game),
                new QuitCommand());
    }

    @Override
    public Location getSize() {
        return Location.of(game.getBoard().getSize(), game.getBoard().getSize());
    }

}
